﻿#region 版本说明
/*****************************************************************************
 * 
 * 项    目 : 
 * 作    者 : 邓胜爽 
 * 创建时间 : 2014年9月22日 14:49:22
 *
 * Copyright (C) 2014 - Alex.Dun.Jason
 * 
 *****************************************************************************/
#endregion
using System;
using System.Xml;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace ImbedLibrary
{
    /// <summary>
    /// XML配置文件帮助类
    /// </summary>
    public class XmlHelper
    {

        public XmlHelper()
        { }

        /// <summary>
        /// 创建空文档
        /// </summary>
        /// <returns></returns>
        public static XmlDocument CreateXml(string sEncoding)
        {
            XmlDocument xml = new XmlDocument();
            XmlDeclaration del = xml.CreateXmlDeclaration("1.0", sEncoding, null);
            xml.AppendChild(del);
            
            return xml;
        }

        /// <summary>
        /// 修改配置文件的节点内容
        /// </summary>
        /// <param name="sNodeName"></param>
        /// <param name="ht"></param>
        /// <returns></returns>
        public static bool SetConfig(string sPath, string sNodeName, Hashtable ht)
        {
            bool result = false;
            try
            {
                foreach (DictionaryEntry item in ht)
                {
                    SetNodeValue(sPath, sNodeName, "Name", item.Key.ToString(), (Hashtable)item.Value);
                }
                result = true;
            }
            catch { }
            return result;
        }

        /// <summary>
        /// 用数据表返回指定节点的xml文档内容
        /// </summary>
        /// <param name="sPath"></param>
        /// <param name="sXmlNode"></param>
        /// <returns></returns>
        public static DataTable GetNodeList(string sPath, string sXmlNode)
        {
            DataSet ds = new DataSet();
            //定义一个操作的xml文档
            XmlDocument doc = new XmlDocument();
            //载入文档
            doc.Load(sPath);
            try
            {
                 StringReader sr = new StringReader(doc.SelectSingleNode("//" + sXmlNode).OuterXml);
                 ds.ReadXml(sr);
                 doc = null;
            }
            catch 
            {
                
            }

            
            return ds.Tables[0];
        }
        /// <summary>
        /// 用数据表返回指定节点的xml文档内容
        /// </summary>
        /// <param name="sXmlContent">源xml内容</param>
        /// <param name="sXmlNode"></param>
        /// <returns></returns>
        public static DataTable GetNodeListFromXml(string sXmlContent, string sXmlNode)
        {
            DataSet ds = new DataSet();
            //定义一个操作的xml文档
            XmlDocument doc = new XmlDocument();
            //载入文档
            doc.LoadXml(sXmlContent);
            StringReader sr = new StringReader(doc.SelectSingleNode("//" + sXmlNode).OuterXml);

            ds.ReadXml(sr);
            doc = null;
            return ds.Tables[0];
        }
        /// <summary>
        /// 返回指定节点的集合
        /// </summary>
        /// <param name="sXmlContent"></param>
        /// <param name="sXmlPath">节点路径</param>
        /// <returns></returns>
        public static XmlNodeList GetNodeListToXml(string sXmlContent, string sXmlPath)
        {
            XmlDocument doc = new XmlDocument();
            //载入文档
            doc.LoadXml(sXmlContent);
            return doc.SelectNodes(sXmlPath);
        }
    
        /// <summary>
        /// 获取指定节点的内容
        /// </summary>
        /// <param name="sPath"></param>
        /// <param name="sXmlNode"></param>
        /// <returns></returns>
        public static string GetNodeValue(string sPath, string sXmlNode)
        {
            string sResult = string.Empty;
            //定义一个文档
            XmlDocument doc = new XmlDocument();
            //载入对象
            doc.Load(sPath);
            XmlNode node = doc.SelectSingleNode("//" + sXmlNode);
            if (node != null)
            {
                sResult = node.InnerText;
            }
            doc = null;
            return sResult;
        }

        /// <summary>
        /// 设置节点的内容
        /// </summary>
        /// <param name="sPath"></param>
        /// <param name="sXmlNode"></param>
        /// <param name="NewValue"></param>
        public static void SetNodeValue(string sPath, string sXmlNode, string NewValue)
        {
            //定义xml文档模型
            XmlDocument doc = new XmlDocument();

            //载入文档
            doc.Load(sPath);
            //获取指定的节点
            XmlNode node = doc.SelectSingleNode("//" + sXmlNode);
            if (node != null)
            {
                node.InnerText = NewValue;
                doc.Save(sPath);
                doc = null;
            }
        }


        /// <summary>
        /// 设置节点的内容
        /// </summary>
        /// <param name="sPath">文档名称</param>
        /// <param name="sXmlNode">节点名称</param>
        /// <param name="sNewValue">节点的值</param>
        /// <param name="AttribList">节点内的属性的Hashtable集合</param>
        public static void SetNodeValue(string sPath, string sXmlNode, string sNewValue, Hashtable AttribList)
        {
            //定义xml文档模型
            XmlDocument doc = new XmlDocument();

            //载入文档
            doc.Load(sPath);
            //获取指定的节点
            XmlNode node = doc.SelectSingleNode("//" + sXmlNode);
            if (node != null)
            {

                for (int i = 0; i < node.Attributes.Count; i++)
                {
                    if (AttribList.Contains(node.Attributes[i].Name))
                    {
                        try
                        {
                            node.Attributes[i].Value = AttribList[node.Attributes[i].Name].ToString();
                        }
                        catch { }
                    }
                }

                if (sNewValue != "")
                {
                    node.InnerText = sNewValue;
                }
                doc.Save(sPath);
                doc = null;
            }
        }
      
        /// <summary>
        /// 设置节点的属性内容（对xml进行修改，未持久化）
        /// </summary>
        /// <param name="xml">要操作的xml内容</param>
        /// <param name="sXmlNode">操作的节点(包括路径)</param>
        /// <param name="sNodeAttribName">节点的属性</param>
        /// <param name="sNodeAttribValue">节点的属性值</param>
        public static void SetNodeValue(ref XmlDocument xml, string sXmlNode, string sNodeAttribName,string sNodeAttribValue)
        {

            //获取指定的节点
            XmlNode node = xml.SelectSingleNode("//" + sXmlNode);
            if (node != null)
            {
              
                try
                {
                    node.Attributes[sNodeAttribName].Value = sNodeAttribValue;
                }
                catch 
                {
                    XmlElement el = node as XmlElement;
                    el.SetAttribute(sNodeAttribName, sNodeAttribValue);
                }
                node = null;
               
                    
            }
        }
        /// <summary>
        /// 设置节点的内容
        /// </summary>
        /// <param name="sPath">文档名称</param>
        /// <param name="sPathNode">父节点名称</param>
        /// <param name="sItemName">要赋值的节点名称</param>
        /// <param name="sNewValue">要赋值的节点名称的值</param>
        /// <param name="AttribList">节点内的属性的Hashtable集合</param>
        public static void SetNodeValue(string sPath, string sPathNode, string sItemName, string sItemValue, Hashtable AttribList)
        {
            //定义xml文档模型
            XmlDocument doc = new XmlDocument();

            //载入文档
            doc.Load(sPath);

            //获取指定的节点
            XmlNode node = doc.SelectSingleNode("//" + sPathNode);
            if (node.HasChildNodes)
            {
                foreach (XmlNode no in node.ChildNodes)
                {
                    if (no.Attributes[sItemName].Value == sItemValue)
                    {
                        foreach (DictionaryEntry item in AttribList)
                        {

                            no.Attributes[item.Key.ToString()].Value = item.Value.ToString();
                        }

                    }
                }
            }


            doc.Save(sPath);
            doc = null;


        }
        /// <summary>
        /// 插入元素
        /// </summary>
        /// <param name="sPath"></param>
        /// <param name="sMainNode">父节点</param>
        /// <param name="sNewNodeName">元素的名称</param>
        /// <param name="AttribList">元素的属性集合</param>
        /// <param name="sNewNodeValue">元素的值</param>
        /// <param name="child">父节点是否是根节点</param>
        public static void InsertNodt(string sPath, string sMainNode, string sNewNodeName, Hashtable AttribList, string sNewNodeValue, bool child)
        {
            //定义xml文档模型
            XmlDocument doc = new XmlDocument();

            //载入文档
            doc.Load(sPath);

            //获取指定的节点
            XmlNode node;
            if (child)
            {
                node = doc.SelectSingleNode("//" + sMainNode);
            }
            else
            {
                node = doc.SelectSingleNode("Root");
            }


            if (node != null)
            {
                XmlElement element = doc.CreateElement(sNewNodeName);
                foreach (DictionaryEntry obj in AttribList)
                {
                    element.SetAttribute(obj.Key.ToString(), obj.Value.ToString());
                }


                if (sNewNodeValue != "")
                {
                    element.InnerText = sNewNodeValue;
                }
                node.AppendChild(element);
                doc.Save(sPath);
                doc = null;
            }
        }
        /// <summary>
        /// 获取节点的全部属性
        /// </summary>
        /// <param name="sPath"></param>
        /// <param name="sNodeName"></param>
        /// <returns></returns>
        public static Hashtable GetAttribs(string sPath, string sNodeName)
        {
            XmlDocument doc = new XmlDocument();
            Hashtable ht = new Hashtable();            
            doc.Load(sPath);
            if (doc.HasChildNodes)
            {
                XmlNode nodes = doc.SelectSingleNode("//" + sNodeName);
                SeachChild(nodes, ref ht);
            }

            return ht;
        }
        public static Hashtable GetAttribsEx(string sXmlText, string sNodeName)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(sXmlText);
            Hashtable ht = new Hashtable();           
            if (doc.HasChildNodes)
            {
                XmlNode nodes = doc.SelectSingleNode("//" + sNodeName);
                SeachChild(nodes, ref ht);
            }

            return ht;
        }

        public static void SeachChild(XmlNode nodes, ref Hashtable ht)
        {
            if (nodes.NodeType == XmlNodeType.Element)
            {
                XmlAttributeCollection attribts = nodes.Attributes;
                if (attribts.Count > 0)
                {
                    foreach (XmlAttribute item in nodes.Attributes)
                    {
                        ht.Add(item.Name, item.Value);
                    }
                }

            }
        }
        /// <summary>
        /// 获取节点的指定属性的值
        /// </summary>
        /// <param name="node"></param>
        /// <param name="attribName"></param>
        /// <returns></returns>
        public static string GetSingleAttrib(XmlNode node, string attribName)
        {
            string sResult = "";
            XmlAttributeCollection attribts = node.Attributes;
            if (attribts.Count > 0)
            {
                foreach (XmlAttribute item in node.Attributes)
                {
                    if (item.Name == attribName)
                    {
                        sResult = item.Value;
                        break;
                    }                   
                }
            }
            return sResult;
        }
        /// <summary>
        /// 获取节点指定属性的值
        /// </summary>
        /// <param name="sPath"></param>
        /// <param name="sNode"></param>
        /// <param name="sAttributeName"></param>
        /// <returns></returns>
        public static string GetSingleAttrib(string sPath, string sNode, string sAttributeName)
        {
            string sResult = "";
            XmlDocument doc = new XmlDocument();                   
            doc.Load(sPath);
            if (doc.HasChildNodes)
            {
                XmlNode nodes = doc.SelectSingleNode("//" + sNode);
                if (nodes != null)
                {
                    XmlAttributeCollection attribts = nodes.Attributes;
                    if (attribts.Count > 0)
                    {
                        foreach (XmlAttribute item in attribts)
                        {
                            if (item.Name == sAttributeName)
                            {
                                sResult = item.Value;
                                break;
                            }
                        }
                    }
                }
              
            }
            return sResult;
          
        }

        /// <summary>
        /// 删除节点
        /// </summary>
        /// <param name="sPath"></param>
        /// <param name="sNodeName"></param>
        /// <param name="sNodeValue"></param>
        /// <returns></returns>
        public static bool DeleteAttrib(string sPath, string sNodeName,string sNodeValue)
        {
            bool result = false;
            XmlDocument doc = new XmlDocument();
            doc.Load(sPath);
            if (doc.HasChildNodes)
            {
                XmlNodeList nodelist = doc.SelectSingleNode("//" + sNodeName).ChildNodes;
                XmlNode node = doc.SelectSingleNode("//" + sNodeName);
                foreach (XmlNode xn in nodelist)
                {
                    XmlElement xel = (XmlElement)xn;
                    XmlAttribute xmlAttribute = xel.GetAttributeNode("Name");

                    if (xmlAttribute.Value == sNodeValue)
                    {
                        node.RemoveChild(xn);
                        break;
                    }
                }
            }
            doc.Save(sPath);
            return result;
        }

    }

}